<?php
require_once 'service.php';
require_once 'util/constants.php';
require_once('model/QuestionnairesManager.php');
require_once('model/UserManager.php');
require_once('exceptions/QuestionnairesException.php');

class QuestionnairesService extends Service {
	
	/* models */
	protected $oQuestionnairesManager;	// connection to surveys collection
	protected $oUserManager;		// connection to users collection
	
    public function __construct() {
        $this->oQuestionnairesManager = new QuestionnairesManager();
        $this->oUserManager = new UserManager();
                        
        parent::__construct('html/questionnaires.html', true);
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getInactiveQuestionnaires() {
   		try {
        	$oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oQuestionnairesManager->getInactiveQuestionnairesAsArrays($oUser), "aoQuestionnaires");
        } catch(QuestionnairesException $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getInactiveQuestionnaire() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oQuestionnairesManager->getInactiveQuestionnaireAsArray($oUser, $this->asParams['id']), "oQuestionnaire");
        } catch(QuestionnairesException $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getActiveQuestionnaire() {
        try {
        	$oUser = new stdClass();
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oQuestionnairesManager->getSurvey($oUser, $this->asParams['id']), "oQuestionnaire");
        } catch(QuestionnairesException $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getNewQuestionnaire() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oQuestionnairesManager->getNewQuestionnaireAsArray($oUser), "oQuestionnaire");
            $this->success();
        } catch(Exception $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function saveNew() {
    	try {
        	$oUser = new stdClass();
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $oQuestionnaireJSON = json_decode($this->asParams['oQuestionnaire']);
            $this->addToResponse($this->oQuestionnairesManager->saveNewQuestionnaire($oUser, $oQuestionnaireJSON), "oQuestionnaire");
            $this->success();
        } catch(QuestionnairesException $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function saveParticipation() {        
        try {
        	$oUser = new stdClass();
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oQuestionnairesManager->saveInactiveQuestionnaire($oUser, $this->asParams), "oQuestionnaire");
            $this->success();
        } catch(QuestionnairesException $se) {
            $this->error($se->getMessage());
        }
    }
}

$QuestionnairesService = new QuestionnairesService();
?>
